//
//  RegisterOrResetViewModel.swift
//  Exchange
//
//  Created by rui on 2018/5/28.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class RegisterOrResetViewModel: BaseViewModel {
    var areaCode: String = ""
    var dynamicCode: String = ""
    var email: String = ""
    var imageCode: String = ""
    var introCode: String = ""
    var loginName: String = ""
    var loginPwd: String = ""
    var mobile: String = ""
    var type: Int = 0 //0 手机 1 邮箱
    
    var imageToken: String = ""
    var sendCodeType: UInt = 2 /// 1 登陆 2 注册 3 找回密码
    
    
    var image: UIImage?
    
    
    lazy var regisetApi: RegisterRequest = {
        let register = RegisterRequest()
        register.dataReformer = self
        register.paramSource = self
        register.delegate = self
        return register
    }()
    
    lazy var mobileCodeApi: MobileCodeRequest =  {
        let mobile = MobileCodeRequest()
        mobile.dataReformer = self
        mobile.paramSource = self
        mobile.delegate = self
        return mobile
    }()
    
    lazy var emailCodeApi: EmailCodeRequest = {
        let email = EmailCodeRequest()
        email.dataReformer = self
        email.paramSource = self
        email.delegate = self
        return email
    }()
    
    lazy var mobileCheckCodeApi: MobileCheckCodeRequest =  {
        let mobile = MobileCheckCodeRequest()
        mobile.dataReformer = self
        mobile.paramSource = self
        mobile.delegate = self
        return mobile
    }()
    
    lazy var emailCheckCodeApi: EmailCheckCodeRequest = {
        let email = EmailCheckCodeRequest()
        email.dataReformer = self
        email.paramSource = self
        email.delegate = self
        return email
    }()
    
    lazy var imageCodeApi: ImageCodeRequest = {
        let image = ImageCodeRequest()
        image.dataReformer = self
        image.paramSource = self
        image.delegate = self
        return image
    }()
    
    lazy var resetPwdApi: ResetPasswordRequest = {
        let reset = ResetPasswordRequest()
        reset.dataReformer = self
        reset.paramSource = self
        reset.delegate = self
        return reset
    }()
    
    init(codeType: Int = 2) {
        self.sendCodeType = UInt(codeType)
        super.init()
    }
    
    func getImageCode() {
        self.imageCodeApi.loadData()
    }
    
    func sendMobileCode() {
        self.mobileCodeApi.loadData()
    }
    
    func sendMobileCode(mobile: String,areaCode: String,imageCode: String) {
        self.mobile = mobile
        self.areaCode = areaCode
        self.imageCode = imageCode
        self.mobileCodeApi.loadData()
    }
    
    func sendEmailCode() {
        self.emailCodeApi.loadData()
    }
    
    func sendEmailCode(email: String,imageCode: String) {
        self.email = email
        self.imageCode = imageCode
        self.emailCodeApi.loadData()
    }
    
    func checkEmailCode(smsCode: String) {
        self.dynamicCode = smsCode
        self.emailCheckCodeApi.loadData()
    }
    func checkMobilCode(smsCode: String) {
        self.dynamicCode = smsCode
        self.mobileCheckCodeApi.loadData()
    }
    
    func register(password: String) {
        self.loginPwd = password
        self.loginName = self.mobile.isEmpty ? self.email : self.mobile
        self.type = self.mobile.isEmpty ? 1 : 0
        self.regisetApi.loadData()
    }
    
    
    func resetPwd(password: String) {
        self.loginPwd = password
        self.loginName = self.mobile.isEmpty ? self.email : self.mobile
        self.type = self.mobile.isEmpty ? 1 : 0
        self.resetPwdApi.loadData()
    }
    
    func decodeImage(data: [String : Any]?) {
        guard let data = data?["data"] as? [String:String] else { return }
        if let imageBase64 = data["imageBase64"],let imageData = Data(base64Encoded: imageBase64) {
            self.image = UIImage(data: imageData)
        }
        if let imageToken = data["imageToken"] {
            self.imageToken = imageToken
        }
    }
}
extension RegisterOrResetViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if let _ = request as? RegisterRequest {
            if let userInfo = data?["data"] as? [String:Any] {
                let appInfo = AppInfo.default
                appInfo.install(data: userInfo)
            }
            self.response?(ResponseState.mine_register,true, nil)
        }else if let _ = request as? MobileCodeRequest {
            self.response?(ResponseState.mine_mobileCode,true, nil)
        }else if let _ = request as? EmailCodeRequest {
            self.response?(ResponseState.mine_emailCode,true, nil)
        }else if let _ = request as? ImageCodeRequest {
            self.decodeImage(data: data)
            self.response?(ResponseState.mine_imageVerify,true, nil)
        }else if let _ = request as? EmailCheckCodeRequest {
            self.response?(ResponseState.mine_checkEmailCode,true, nil)
        }else if let _ = request as? MobileCheckCodeRequest {
            self.response?(ResponseState.mine_checkMobileCode,true, nil)
        }else if let _ = request as? ResetPasswordRequest {
            if let userInfo = data?["data"] as? [String:Any] {
                let appInfo = AppInfo.default
                appInfo.install(data: userInfo)
            }
            self.response?(ResponseState.mine_resetPwd,true, nil)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if let _ = request as? RegisterRequest {
            self.response?(ResponseState.mine_register,false, request.errorMsg)
        }else if let _ = request as? MobileCodeRequest {
            self.response?(ResponseState.mine_mobileCode,false, request.errorMsg)
        }else if let _ = request as? EmailCodeRequest {
            self.response?(ResponseState.mine_emailCode,false, request.errorMsg)
        }else if let _ = request as? ImageCodeRequest {
            self.response?(ResponseState.mine_imageVerify,false, request.errorMsg)
        }else if let _ = request as? EmailCheckCodeRequest {
            self.response?(ResponseState.mine_checkEmailCode,false, request.errorMsg)
        }else if let _ = request as? MobileCheckCodeRequest {
            self.response?(ResponseState.mine_checkMobileCode,false, request.errorMsg)
        }else if let _ = request as? ResetPasswordRequest {
            self.response?(ResponseState.mine_resetPwd,false, request.errorMsg)
        }
    }

    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? RegisterRequest {
            pramas.updateValue(sendCodeType, forKey: "codeType")
            if !introCode.isEmpty {
                pramas.updateValue(introCode, forKey: "introCode")
            }
            if type == 0 {
                pramas.updateValue(dynamicCode, forKey: "mobileCode")
            }else if type == 1 {
                pramas.updateValue(dynamicCode, forKey: "emailCode")
            }
            pramas.updateValue(loginName, forKey: "loginName")
            pramas.updateValue(loginPwd, forKey: "loginPwd")
            pramas.updateValue(type, forKey: "type")
            pramas.updateValue(areaCode, forKey: "areaCode")
        }else if let _ = request as? MobileCodeRequest {
            pramas.updateValue(areaCode, forKey: "areaCode")
            pramas.updateValue(imageCode, forKey: "imageCode")
            pramas.updateValue(imageToken, forKey: "imageToken")
            pramas.updateValue(mobile, forKey: "mobile")
            pramas.updateValue(sendCodeType, forKey: "codeType")
        }else if let _ = request as? EmailCodeRequest {
            pramas.updateValue(email, forKey: "email")
            pramas.updateValue(imageCode, forKey: "imageCode")
            pramas.updateValue(imageToken, forKey: "imageToken")
            pramas.updateValue(sendCodeType, forKey: "codeType")
        }else if let _ = request as? EmailCheckCodeRequest {
            pramas.updateValue(email, forKey: "email")
            pramas.updateValue(imageCode, forKey: "imageCode")
            pramas.updateValue(imageToken, forKey: "imageToken")
            pramas.updateValue(sendCodeType, forKey: "codeType")
            pramas.updateValue(dynamicCode, forKey: "emailCode")
        }else if let _ = request as? MobileCheckCodeRequest {
            pramas.updateValue(areaCode, forKey: "areaCode")
            pramas.updateValue(imageCode, forKey: "imageCode")
            pramas.updateValue(imageToken, forKey: "imageToken")
            pramas.updateValue(mobile, forKey: "mobile")
            pramas.updateValue(sendCodeType, forKey: "codeType")
            pramas.updateValue(dynamicCode, forKey: "mobileCode")
        }else if let _ = request as? ResetPasswordRequest {
            pramas.updateValue(sendCodeType, forKey: "codeType")
            pramas.updateValue(loginPwd, forKey: "password")
            pramas.updateValue(type, forKey: "type")
            if type == 0 {
                pramas.updateValue(mobile, forKey: "mobile")
                pramas.updateValue(dynamicCode, forKey: "mobileCode")
            }else if type == 1 {
                pramas.updateValue(email, forKey: "email")
                pramas.updateValue(dynamicCode, forKey: "emailCode")
            }
        }else if let _ = request as? ImageCodeRequest {
            
        }
        return pramas
    }
 
}
